package com.qf.base.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author 汤奇
 * @version V1.0
 * @Project BookPro
 * @Package com.qf.base.util
 * @Description: 手动获取IOC容器内部对象的工具类
 * @Date 2022/8/8 19:58
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {
    /**
     * 应用程序上下文对象
     * 获取IOC容器内部对象的时候, 就要使用它
     */
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }

    public static <T> T getBean(Class<T> clazz){
        return applicationContext.getBean(clazz);
    }
}
